# -*- conding:UTF-8 -*-

'''
@ Project: p7_flask
@ File: jwt_auth.py
@ Author: 师路遥
@ Date：2022/6/6 19:53

'''

from flask import request, g
from common.utils.jwt_util import check_token

def jwt_authentication():
    """
    验证token
    在获取用户信息前验证前端发起请求携带的token,是否和后端生成的一样
    """
    token = request.headers.get('Authorization')
    if token:
        payload = check_token(token)
        print('payload--->', payload)
        if payload:
            # token 校验成功, 把用户id 写入g 对象
            print('g.user_id--->',payload.get('id'))
            g.user_id = payload.get('id')